package com.doit.doit47;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;

import java.time.Duration;
import java.util.Arrays;

public class _03_消费者指定从哪里开始读取数据 {
    public static void main(String[] args) {
        KafkaConsumer<String, String> consumer = MyKafkaUtils.getConsumer("luo_guo", false, "earliest");

        //这种订阅主题的方式是一下子订阅整个topic，所有分区都是被这个消费者所消费的
//        consumer.subscribe(Arrays.asList("userinfo"));
        TopicPartition topicPartition0 = new TopicPartition("userinfo", 0);
        TopicPartition topicPartition1 = new TopicPartition("userinfo", 1);
        TopicPartition topicPartition2 = new TopicPartition("userinfo", 2);
        consumer.assign(Arrays.asList(topicPartition0,topicPartition1,topicPartition2));
        //指定从哪里开始读取数据
        consumer.seek(topicPartition0,1);
        consumer.seek(topicPartition1,2);
        consumer.seek(topicPartition2,2);

        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> record : poll) {
                int partition = record.partition();
                long offset = record.offset();
                String value = record.value();
                System.out.println(partition+","+offset+","+value);
            }
        }

    }
}
